package com.bluestar.tools.module.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;

import com.bluestar.tools.R;
import com.bluestar.tools.module.camera.CameraProvide;
import com.bluestar.tools.utils.SystemHelper;
import com.bluestar.tools.utils.ThrowableUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : lifei
 * 创建时间： 19-10-10
 * 更改时间： 19-10-10
 * Email : allen.lee723@qq.com
 * @version :
 * Description：MainPresent
 */
public class MainPresenter {

    private MainView mainView;
    private StringBuilder stringBuilder;

    public MainPresenter(MainView mainView) {
        this.mainView = mainView;
        stringBuilder = new StringBuilder();
    }

    //  进入相机
    public void goToCameraActivity() {
        mainView.goToCameraActivity();
    }

    public void getMainList(Context context) {
        List<MainBean> mainBeanList = new ArrayList<>();
        mainBeanList.add(new MainBean(ItemType.GO_TO_CAMERA, context.getText(R.string.goto_camera).toString()));
        mainBeanList.add(new MainBean(ItemType.GET_DIR, context.getText(R.string.get_dir).toString()));
        mainBeanList.add(new MainBean(ItemType.DEVICE_INFO, context.getText(R.string.device_info).toString()));
        mainBeanList.add(new MainBean(ItemType.CREATE_FILE, context.getText(R.string.create_file).toString()));
        mainBeanList.add(new MainBean(ItemType.CAMERA_INFO, context.getText(R.string.camera_info).toString()));
        mainBeanList.add(new MainBean(ItemType.CHANGE_ORIENTATION, context.getText(R.string.change_orientation).toString()));
        mainBeanList.add(new MainBean(ItemType.BUGLY_CRASH, context.getText(R.string.send_bugly_crash).toString()));
        mainBeanList.add(new MainBean(ItemType.GET_IMEI, context.getText(R.string.get_imei).toString()));
        mainBeanList.add(new MainBean(ItemType.FOOTER_LIST, context.getText(R.string.goto_footer_view).toString()));
        mainBeanList.add(new MainBean(ItemType.DEVICE_MANAGER, context.getText(R.string.device_manager).toString()));
        mainBeanList.add(new MainBean(ItemType.SYSTEM_SETTINGS, context.getText(R.string.system_settings).toString()));
        mainView.updateList(mainBeanList);
    }

    // 获取目录
    public void getDir(Context context) {
        stringBuilder.delete(0, stringBuilder.length());
        stringBuilder
            .append("Environment.getExternalStorageDirectory().getPath() = ")
            .append(Environment.getExternalStorageDirectory().getPath())
            .append("\n")
            .append("Environment.getExternalStorageDirectory().getAbsolutePath() = ")
            .append(Environment.getExternalStorageDirectory().getAbsolutePath())
            .append("\n")
            .append("Environment.getRootDirectory().getPath() = ")
            .append(Environment.getRootDirectory().getPath())
            .append("\n")
            .append("Environment.getRootDirectory().getAbsolutePath() = ")
            .append(Environment.getRootDirectory().getAbsolutePath())
            .append("\n")
            .append("getFilesDir().getAbsolutePath() = ")
            .append(context.getFilesDir().getAbsolutePath())
            .append("\n")
            .append("SD 卡挂载状态 = ")
            .append(Environment.getExternalStorageState())
            .append("\n");
        mainView.updateUI(stringBuilder.toString());
    }

    // 获取设备信息
    public void getDeviceInfo() {
        stringBuilder.delete(0, stringBuilder.length());
        stringBuilder
            .append("app的名称: ").append(SystemHelper.appName()).append("\n")
            .append("app的版本号: ").append(SystemHelper.versionCode()).append("\n")
            .append("app的版本号名: ").append(SystemHelper.versionName()).append("\n")
            .append("app的包名: ").append(SystemHelper.packgeName()).append("\n")
            .append("手机产品的序列号: ").append(SystemHelper.SN()).append("\n")
            .append("手机的ID: ").append(SystemHelper.ID()).append("\n")
            .append("手机的mac地址: ").append(SystemHelper.mac()).append("\n")
            .append("手机的ip地址: ").append(SystemHelper.ip()).append("\n")
            .append("系统国家: ").append(SystemHelper.country()).append("\n")
            .append("系统语言: ").append(SystemHelper.language()).append("\n")
            .append("屏幕的高: ").append(SystemHelper.height()).append("\n")
            .append("屏幕的宽: ").append(SystemHelper.width()).append("\n")
            .append("系统定义的xdpi：").append(SystemHelper.getXdpi()).append("\n")
            .append("系统定义的ydpi：").append(SystemHelper.getYdpi()).append("\n")
            .append("屏幕最小宽度: ").append(SystemHelper.smallestScreenWidth()).append("\n")
            .append("系统版本名: ").append(Build.VERSION.RELEASE).append("\n")
            .append("系统版本号: ").append(Build.VERSION.SDK_INT).append("\n")
            .append("系统型号: ").append(Build.MODEL).append("\n")
            .append("系统定制商: ").append(Build.BRAND).append("\n")
            .append("系统的主板: ").append(Build.BOARD).append("\n")
            .append("手机制造商: ").append(Build.PRODUCT).append("\n");
        mainView.updateUI(stringBuilder.toString());
    }

    // 创建文件
    public void createFile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File file = new File("/mnt/usbhost/", "aaa.txt");
                stringBuilder.delete(0, stringBuilder.length());
                if (!file.exists()) {
                    stringBuilder.append(file.getPath()).append(" 不存在，正在创建...").append("\n");
                    try {
                        if (file.createNewFile()) {
                            stringBuilder.append("创建 ").append(file.getPath()).append(" 成功").append("\n");
                        } else {
                            stringBuilder.append("创建 ").append(file.getPath()).append(" 失败").append("\n");
                        }
                    } catch (IOException e) {

                        stringBuilder.append("创建 ").append(file.getPath()).append(" 失败").append("\n\n");
                        stringBuilder.append(ThrowableUtils.toString(e)).append("\n");
                        e.printStackTrace();

                    } finally {
                        mainView.getHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                mainView.updateUI(stringBuilder.toString());
                            }
                        });
                    }
                } else {
                    stringBuilder.append(file.getPath()).append(" 已经创建，请勿重复创建！").append("\n");
                    mainView.getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            mainView.updateUI(stringBuilder.toString());
                        }
                    });
                }
            }
        }).start();
    }

    // 检测相机
    public void checkCamera() {
        stringBuilder.delete(0, stringBuilder.length());
        stringBuilder
            .append("设备是否有摄像头: ").append(CameraProvide.hasCamera()).append("\n")
            .append("设备是否有后置摄像头: ").append(CameraProvide.hasBackFacingCamera()).append("\n")
            .append("设备是否有前置摄像头: ").append(CameraProvide.hasFrontFacingCamera()).append("\n")
            .append("相机是否可用: ").append(CameraProvide.isCameraCanUse()).append("\n")
            .append("摄像头的数量：").append(CameraProvide.getCameraNumber()).append("\n");
        mainView.updateUI(stringBuilder.toString());
    }

    // 发送bugly异常
    public void sendBuglyCrash() {
        //CrashReport.testJavaCrash();
    }

    // 判断文件是否存在
    public boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }

        } catch (Exception e) {
            return false;
        }
        return true;
    }

    // 横竖屏切换
    public void changeOrientation() {
        mainView.changeOrientation();
    }

    public void getIMEI(Context context) {
        stringBuilder.delete(0, stringBuilder.length());
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String imei = tm.getDeviceId();
        stringBuilder.append("IMEI: ").append(imei);
        mainView.updateUI(stringBuilder.toString());
    }

    public void gotoFooterView() {
        mainView.gotoFooterView();
    }

    public void deviceManager() {
        mainView.deviceManager();
    }

    public void systemSettings() {
        mainView.systemSettings();
    }
}
